{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "下界阈值: 60.97\n",
      "上界阈值: 139.02\n",
      "异常值数量: 13\n",
      "异常值: [ 60.70382344 140.8025375  157.79097236  51.3809899  146.18321213\n",
      "  59.54670036  60.23545287 139.48573097 200.          10.\n",
      " 250.           0.         180.        ]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 生成示例数据\n",
    "np.random.seed(42)#设置随机种子，保证每次运行结果相同\n",
    "data = np.random.normal(100, 15, 1000)  # 生成1000个正态分布的数据，100是均值，15是标准差\n",
    "# 添加一些异常值\n",
    "data = np.append(data, [200, 10, 250, 0, 180])#添加一些异常值\n",
    "\n",
    "# 计算四分位数\n",
    "Q1 = np.percentile(data, 25)  # 第一四分位数\n",
    "Q3 = np.percentile(data, 75)  # 第三四分位数\n",
    "IQR = Q3 - Q1  # 四分位距\n",
    "\n",
    "# 定义异常值的阈值\n",
    "lower_bound = Q1 - 1.5 * IQR\n",
    "upper_bound = Q3 + 1.5 * IQR\n",
    "\n",
    "# 找出异常值\n",
    "outliers = data[(data < lower_bound) | (data > upper_bound)]\n",
    "\n",
    "\n",
    "\n",
    "print(f\"下界阈值: {lower_bound:.2f}\")\n",
    "print(f\"上界阈值: {upper_bound:.2f}\")\n",
    "print(f\"异常值数量: {len(outliers)}\")\n",
    "print(\"异常值:\", outliers)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "335e850e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Z-score方法检测结果:\n",
      "异常值数量: 6\n",
      "异常值: [157.79097236 200.          10.         250.           0.\n",
      " 180.        ]\n"
     ]
    }
   ],
   "source": [
    "# 使用Z-score方法检测异常值\n",
    "z_scores = (data - np.mean(data)) / np.std(data)#data是数据，np.mean(data)是数据均值，np.std(data)是数据标准差\n",
    "\n",
    "# 通常将|Z-score|>3视为异常值\n",
    "z_score_outliers = data[np.abs(z_scores) > 3]\n",
    "\n",
    "print(\"\\nZ-score方法检测结果:\")\n",
    "print(f\"异常值数量: {len(z_score_outliers)}\")\n",
    "print(\"异常值:\", z_score_outliers)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "412477ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "DBSCAN方法检测结果:\n",
      "异常值数量: 5\n",
      "异常值: [200.  10. 250.   0. 180.]\n"
     ]
    }
   ],
   "source": [
    "# 使用DBSCAN方法检测异常值\n",
    "from sklearn.cluster import DBSCAN\n",
    "import numpy as np\n",
    "\n",
    "# 将数据重塑为二维数组(DBSCAN需要二维数组输入)\n",
    "X = data.reshape(-1, 1)\n",
    "\n",
    "# 创建DBSCAN模型\n",
    "# eps是邻域半径，min_samples是成为核心点所需的最小样本数\n",
    "dbscan = DBSCAN(eps=20, min_samples=5)\n",
    "clusters = dbscan.fit_predict(X)\n",
    "\n",
    "# -1表示异常点\n",
    "dbscan_outliers = data[clusters == -1]\n",
    "\n",
    "print(\"\\nDBSCAN方法检测结果:\")\n",
    "print(f\"异常值数量: {len(dbscan_outliers)}\")\n",
    "print(\"异常值:\", dbscan_outliers)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3cb2eca0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Isolation Forest方法检测结果:\n",
      "异常值数量: 100\n",
      "异常值: [ 71.30079633  74.12623251  70.60494814  73.55439767  60.70382344\n",
      "  70.18646628 128.29278852  71.21843177 136.94863169 132.85683439\n",
      "  76.74004853  75.88775148 127.98661767 128.45189474 140.8025375\n",
      " 157.79097236 134.7198785   71.99102211  75.80926193 132.15916134\n",
      "  69.6228612  126.4818136  131.83234296  51.3809899  124.48616956\n",
      " 131.99550062  70.71868301 131.38580914 132.29773686 132.847044\n",
      "  68.14156414 128.14256259  74.30298206  76.08358512 130.91121887\n",
      " 126.33011264  73.6189077   69.41151733 124.42923318  74.44926341\n",
      "  68.9883685  129.47087699  65.47118253 124.6745157  146.18321213\n",
      "  75.9033052  125.30712453 128.64124961  74.95892078  72.92676849\n",
      "  75.58686343  75.07719907  62.9253325  131.13101198  69.42813197\n",
      "  71.93812118 125.14655968  66.83297036 134.06039287 128.15193719\n",
      "  73.31919627 136.68627969 128.23036745  74.61303055  75.17714992\n",
      " 125.03532288  59.54670036 138.60039705  60.23545287  68.89914651\n",
      " 126.28405665  76.6505624  126.32191273  68.77105888 125.44684552\n",
      "  74.34747411  74.46124594 124.98211667  72.38688653 139.48573097\n",
      " 138.40126807 126.56200953  76.4466292   75.085586   134.48347185\n",
      " 136.8295021  130.15306808 137.90398639 132.44882085 128.21735604\n",
      "  67.01791065  71.65688904  63.6418101   76.24145765  69.37397697\n",
      " 200.          10.         250.           0.         180.        ]\n"
     ]
    }
   ],
   "source": [
    "# 使用Isolation Forest方法检测异常值\n",
    "from sklearn.ensemble import IsolationForest\n",
    "\n",
    "# 将数据重塑为二维数组\n",
    "X = data.reshape(-1, 1)\n",
    "\n",
    "# 创建并训练Isolation Forest模型\n",
    "# contamination参数表示预期的异常值比例，可以根据实际情况调整\n",
    "iso_forest = IsolationForest(contamination=0.1, random_state=42)\n",
    "predictions = iso_forest.fit_predict(X)\n",
    "\n",
    "# -1表示异常值，1表示正常值\n",
    "isolation_forest_outliers = data[predictions == -1]\n",
    "\n",
    "print(\"\\nIsolation Forest方法检测结果:\")\n",
    "print(f\"异常值数量: {len(isolation_forest_outliers)}\")\n",
    "print(\"异常值:\", isolation_forest_outliers)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
